package com.yonyou.pmclouds.billview.plugin;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.workConnect.entity.WorkConBillStatus;
import com.yonyou.pmclouds.workConnect.entity.WorkConReceiverVO;
import com.yonyou.pmclouds.workConnect.entity.WorkConnectVO;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class WorkConnViewPlugin implements IBillViewCustomPlugin {

    @Autowired
    private UserInfoQuery userInfoQuery;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    public Object[] fillListBuziField(Object[] objects) throws BusinessException {

        List<String> pkReceiverList = new ArrayList<String>();
        WorkConnectVO[] workConnectVOs = new WorkConnectVO[objects.length];
        userInfoQuery.tranUserInfoField(objects,new String[]{CommonFieldConst.CREATOR},new String[]{WorkConnectVO.CREATOR_NAME});

        for (int i = 0 ; i <workConnectVOs.length ; i++){
            workConnectVOs[i] = (WorkConnectVO) objects[i];
        }
        for (WorkConnectVO workConnectVO :
                workConnectVOs) {
            String[] pkReceivers = workConnectVO.getPkReceivers();
            for (String pkReceiver :
                    pkReceivers) {
                pkReceiverList.add(pkReceiver);
            }
        }
        Map<String,String> userIdAndNameMap = userInfoQuery.queryUserNames(pkReceiverList.toArray(new String[0]));

        for (WorkConnectVO workConnectVO :
                workConnectVOs) {
            String[] pkReceivers = workConnectVO.getPkReceivers();
            String[] receiverNames = new String[pkReceivers.length];
            for (int i = 0 ;i<pkReceivers.length ; i++){
                receiverNames[i] = userIdAndNameMap.get(pkReceivers[i]);
            }
            workConnectVO.setReceiverNames(receiverNames);
        }

        return workConnectVOs;

    }

    @Override
    public Map<String, Object[]> fillDetailBuziField(SuperVO superVO, HttpServletRequest request) throws BusinessException {
        WorkConnectVO workConnectVO = ((WorkConnectVO)superVO);

        userInfoQuery.tranUserInfoField(new WorkConnectVO[]{workConnectVO} ,new String[]{CommonFieldConst.CREATOR},new String[]{WorkConnectVO.CREATOR_NAME});

        WorkConReceiverVO[] workConReceiverVOs = workConnectVO.getWorkConReceiverVOS();

        // 已接收的接收信息
        List<WorkConReceiverVO> receivedList = new ArrayList<WorkConReceiverVO>();

        if(ArrayUtils.isNotEmpty(workConReceiverVOs)){
            String[] pkReceivers = new String[workConReceiverVOs.length];

            for (int i=0 ; i<workConReceiverVOs.length ; i++){
                pkReceivers[i] = workConReceiverVOs[i].getPkReceiver();
            }

            Map<String,String> userIdAndNameMap = userInfoQuery.queryUserNames(pkReceivers);

            StringBuffer sb = new StringBuffer();


            for (WorkConReceiverVO workConReceiverVO :
                    workConReceiverVOs) {
                workConReceiverVO.setReceiverName(userIdAndNameMap.get(workConReceiverVO.getPkReceiver()));
                sb.append(userIdAndNameMap.get(workConReceiverVO.getPkReceiver())+",");
                if(WorkConReceiverVO.RECEIVE_STATUS_UNACCEPT == workConReceiverVO.getReceiveStatus()){
                    workConReceiverVO.setStrReceiveStatus("未接收");
                }else{
                    workConReceiverVO.setStrReceiveStatus("已接收");
                    if(workConReceiverVO.getReceivedTime() != null){
                        workConReceiverVO.setStrReceiveStatus(sdf.format(workConReceiverVO.getReceivedTime()));
                    }
                    workConReceiverVO.setPk_workconnect(workConReceiverVO.getPkWorkconnect());
                    receivedList.add(workConReceiverVO);
                }
            }
            String receivers = sb.toString().substring(0,sb.toString().lastIndexOf(","));
            workConnectVO.setReceiverName(receivers);
        }

        if(WorkConBillStatus.STATUS_ACCEPTED == workConnectVO.getBillstatus()){
            workConnectVO.setStrBillstatus("已接收");
        }else{
            workConnectVO.setStrBillstatus("待接收");
        }

        Map<String,Object[]> resultMap = new HashMap<String,Object[]>();

        resultMap.put(BusinessObjectConsts.PM_WORKCONNECT, new Object[]{superVO});
        if(receivedList!=null && receivedList.size() > 0){
            resultMap.put("pm_workconnect_receiver", receivedList.toArray(new WorkConReceiverVO[0]));
        }
        return resultMap;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.WORKCONNECT_BILLTYPE;
    }

}
